package com.jia.train.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jia.train.global.context.AuthContextHolder;
import com.jia.train.global.page.PageResponse;
import com.jia.train.global.utils.SnowIdUtil;
import com.jia.train.member.entity.Passenger;
import com.jia.train.member.entity.PassengerExample;
import com.jia.train.member.mapper.PassengerMapper;
import com.jia.train.member.request.PassengerQueryRequest;
import com.jia.train.member.request.PassengerSaveRequest;
import com.jia.train.member.response.PassengerQueryResponse;
import com.jia.train.member.response.PassengerSaveResponse;
import com.jia.train.member.service.PassengerService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PassengerServiceImpl implements PassengerService {

    @Resource
    private PassengerMapper passengerMapper;

    @Override
    public PassengerSaveResponse insert(PassengerSaveRequest request) {
        Passenger passenger = BeanUtil.copyProperties(request, Passenger.class);
        DateTime now = DateTime.now();
        if (ObjectUtil.isEmpty(passenger.getId())) {
            passenger.setMemberId(AuthContextHolder.getId());
            passenger.setId(SnowIdUtil.getLongSnowNextId());
            passenger.setCreateTime(now);
            passengerMapper.insert(passenger);
        } else {
            passenger.setUpdateTime(now);
            passengerMapper.updateByPrimaryKeySelective(passenger);
        }

        return BeanUtil.copyProperties(passenger, PassengerSaveResponse.class);
    }

    @Override
    public PageResponse<PassengerQueryResponse> selectPage(PassengerQueryRequest request) {

        PassengerExample passengerExample = new PassengerExample();
        PassengerExample.Criteria criteria = passengerExample.createCriteria();
        criteria.andMemberIdEqualTo(AuthContextHolder.getId());

        // 构造分页
        PageHelper.startPage(request.getPage(), request.getSize());
        List<Passenger> passengers = passengerMapper.selectByExample(passengerExample);
        PageInfo<Passenger> pageInfo = new PageInfo<>(passengers);

        List<PassengerQueryResponse> list
                = BeanUtil.copyToList(passengers, PassengerQueryResponse.class);

        PageResponse<PassengerQueryResponse> pageResponse = new PageResponse<>();
        pageResponse.setTotal(pageInfo.getTotal());
        pageResponse.setList(list);
        return pageResponse;
    }

    @Override
    public void deletePassenger(Long id) {
        passengerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<PassengerQueryResponse> selectList() {
        PassengerExample passengerExample = new PassengerExample();
        PassengerExample.Criteria criteria = passengerExample.createCriteria();
        criteria.andMemberIdEqualTo(AuthContextHolder.getId());
        List<Passenger> passengers =
                passengerMapper.selectByExample(passengerExample);
        return BeanUtil.copyToList(passengers, PassengerQueryResponse.class);
    }


}
